package com.it.tydic.estate.module.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.util.*;
import com.it.tydic.estate.module.mapper.inner.GuanlichuMapper;
import com.it.tydic.estate.module.model.inner.Guanlichu;
import com.it.tydic.estate.module.service.LouyuService;
import com.it.tydic.estate.module.service.XqglService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;
import java.util.*;

/**
 * 房管处实现类
 */
@Service
@Transactional
public class XqglServiceImpl implements XqglService {
    @Resource
    private GuanlichuMapper guanlichuMapper;////小区
    @Autowired
    private LouyuService louyuService;//楼宇

    /**
     * 根据条件查询数据
     *
     * @param guanlichu
     * @return
     */
    @Override
    public Result queryList(Map<String, Object> guanlichu) {
        Map<String, Object> map = new HashMap<>();
        if (guanlichu.containsKey("pageNum") && guanlichu.containsKey("pageSize")) {//判断是否有分页条件有进行分页
            PageHelper.startPage((Integer) guanlichu.get("pageNum"), (Integer) guanlichu.get("pageSize"));
        }else{
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        Page<Guanlichu> xiaoquguanlis = guanlichuMapper.selectList(guanlichu);
        List<Guanlichu> result = xiaoquguanlis.getResult();
        map.put("result", result);
        map.put("totle", xiaoquguanlis.getTotal());
        return Result.success(map);

    }

    /**
     * 小区管理添加或修改
     *
     * @param guanlichu
     * @return
     */
    @Override
    public Result addOrUpload(Guanlichu guanlichu) {
        if(StringUtil.isEmpty(guanlichu.getName())){//参数非空验证
            return  Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        String userId= CurrentUtil.getCurrentUserId();
        if (guanlichu.getId() == null || "".equals(guanlichu.getId())) {//判断是否有主键
            long id = new InstaIdGenerator().nextId(TimeUtil.timeGen());//生成Id主键
           /* boolean flag = checkCode(guanlichu.getCode());//编码验证
            if(flag)
                return  Result.error(CodeMsg.DATA_REPEATE);*/
            guanlichu.setId(String.valueOf(id));
            guanlichu.setCjsj(new Date());
            guanlichu.setXgsj(new Date());
            guanlichu.setCuserId(userId);
            guanlichu.setZfbj(ZfbjFinalUtil.zfbj_zy);
            int result = guanlichuMapper.insertSelective(guanlichu);
            if (result < 0)
                return Result.error(CodeMsg.INTER_ERROR);
        } else {
            guanlichu.setXgsj(new Date());
            guanlichu.setUuserId(userId);
            int result = guanlichuMapper.updateByPrimaryKeySelective(guanlichu);
            if (result < 0){
                return Result.error(CodeMsg.UPDATE_ERROR);
            }
        }
        return Result.success(guanlichu.getId());

    }

    /**
     * 根据主键删除
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteById(String id) {
        if (!StringUtils.isNotEmpty(id))//非空校验
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        Guanlichu guanlichu = new Guanlichu();
        guanlichu.setId(id);//添加删除条件
        guanlichu.setZfbj(ZfbjFinalUtil.zfbj_zf);
        int result = guanlichuMapper.updateByPrimaryKeySelective(guanlichu);
        if (result <= 0)
            return Result.error(CodeMsg.DELETE_ERROR);
        ArrayList<String> objects = new ArrayList<String>();
        objects.add(id);
        louyuService.deleteBatchByXqid(objects);//删除楼宇信息
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @Override
    public Result deleteBatch(List<String> list) {
        if (list == null || list.size() <= 0)//非空校验
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        int result = guanlichuMapper.deleteBatch(list);
        if (result <= 0)
            return Result.error(CodeMsg.DELETE_ERROR);
        louyuService.deleteBatchByXqid(list);//删除楼宇信息
        return Result.success();
    }

    /**
     * 查询小区和小区所属楼宇
     *
     * @return
     */
    @Override
    public Result queryTree() {
        List<Guanlichu> result = guanlichuMapper.queryTree();
        return Result.success(result);
    }

    /**
     * 根据主键查询小区
     *
     * @param id
     * @return
     */
    @Override
    public Result queryById(String id) {
        if (!StringUtils.isNotEmpty(id))//非空校验
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        Guanlichu guanlichu = guanlichuMapper.selectByPrimaryKey(id);
        if(guanlichu==null){
            return Result.error(CodeMsg.DATA_NOT_EXIST);
        }
        return Result.success(guanlichu);
    }

    /**
     * 查询小区
     * @return
     */
    @Override
    public Result queryXqListt() {
        Example example = new Example(Guanlichu.class);
        example.createCriteria().andEqualTo("zfbj", ZfbjFinalUtil.zfbj_zy);
        List<Guanlichu> guanlichus = guanlichuMapper.selectByExample(example);
        return Result.success(guanlichus);
    }

    /**
     * 验证编码小区是否重复
     * @param code
     * @return
     */
    private boolean checkCode(String code) {
        boolean flag =false;
        Example example = new Example(Guanlichu.class);
        example.createCriteria().andEqualTo("code", code).andEqualTo("zfbj", ZfbjFinalUtil.zfbj_zy);
        int num = guanlichuMapper.selectCountByExample(example);
        if (num > 0)
            flag=true;
        return flag;
    }
}
